﻿using System;
using System.Collections.Generic;
using Monti.MVVM;
using Peter.Common;
using Peter.Data.Options;
using Peter.Logic.Plugins;

namespace Peter.Logic
{
   /// <summary>
   /// Manages all the options for peter.
   /// </summary>
   public class OptionsManager : ViewModelBase, IPeterOptions
   {
      /// <summary>
      /// Initializes a new options manager object.
      /// </summary>
      public OptionsManager ()
      {
         this.Fonts = new PeterFonts ();
         this.EditorOptions = new EditorOptions ();
         this.InstalledPlugins = new List <Plugin> ();
         this.ExtensionOpeners = new Dictionary <string, string> ();
      }

      /// <summary>
      /// Gets the font options for peter.
      /// </summary>
      public IFontOptions Fonts { get; private set; }

      /// <summary>
      /// Gets the editor options.
      /// </summary>
      public EditorOptions EditorOptions { get; private set; }

      /// <summary>
      /// Gets a list of installed plugins.
      /// </summary>
      public List <Plugin> InstalledPlugins { get; private set; }

      /// <summary>
      /// Gets the list of extensions are to be opened by different plugins.
      /// </summary>
      public Dictionary <string, string> ExtensionOpeners { get; private set; }

      /// <summary>
      /// Registers a plugin to open the given extension.
      /// </summary>
      /// <param name="pluginName">Name of plugin that wants to open the extension.</param>
      /// <param name="extension">Extension to open.</param>
      public void RegisterToOpenExtension (string pluginName, string extension)
      {
         extension = extension.Replace ("*", string.Empty);
         if (!extension.Contains ("."))
            extension = "." + extension;
         if (!extension.StartsWith ("."))
            extension = extension.Substring (extension.IndexOf(".", StringComparison.Ordinal));

         if (!this.ExtensionOpeners.ContainsKey (extension))
            this.ExtensionOpeners.Add (extension, pluginName);
         else
         {
            // TODO: Notify user about change...
         }
      }
   }
}
